package com.black.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.AppUser;
import com.black.app.domain.my.AppMyDynamicImgOrVideo;
import com.black.app.domain.my.AppMyFansFollow;
import com.black.app.domain.my.AppMyUserdynamic;
import com.black.app.domain.my.AppMyUserinfo;
import com.black.app.domain.req.my.MyUserPostEditReq;
import com.black.app.domain.req.my.MyUserPostInformationEditReq;
import com.black.app.domain.req.my.MyUserPostInformationReq;
import com.black.app.domain.resp.my.MyUserPostHomeResp;
import com.black.app.domain.resp.my.MyUserPostInformationResp;
import com.black.app.domain.resp.my.MyUserPostResp;
import com.black.app.mapper.AppUserMapper;
import com.black.app.mapper.my.AppMyDynamicImgOrVideoMapper;
import com.black.app.mapper.my.AppMyFansFollowMapper;
import com.black.app.mapper.my.AppMyUserdynamicMapper;
import com.black.app.mapper.my.AppMyUserinfoMapper;
import com.black.app.service.AppUserService;
import com.black.app.service.my.AppMyDynamicImgOrVideoService;
import com.black.app.service.my.AppMyFansFollowService;
import com.black.app.service.my.AppMyUserdynamicService;
import com.black.app.service.my.AppMyUserinfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author tonneyyy
 * @description 针对表【app_user(手机端用户)】的数据库操作Service实现
 * @createDate 2024-09-28 15:01:49
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser>
        implements AppUserService {



    @Autowired
    private AppMyUserinfoService appMyUserinfoService;

    @Autowired
    private AppMyUserdynamicService appMyUserdynamicService;

    @Autowired
    private AppMyUserdynamicMapper appMyUserdynamicMapper;

    @Autowired
    private AppMyDynamicImgOrVideoMapper appMyDynamicImgOrVideoMapper;

    /**
     * 根据手机号查询用户信息
     *
     * @param phone
     * @return
     */
    @Override
    public AppUser findByPhone(String phone) {
        LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUser::getPhone, phone);
        return this.getOne(queryWrapper);
    }


    /**
     * 分页查询用户信息
     * @param req
     * @return
     */
    @Override
    public PageInfo<MyUserPostInformationResp> postInformation(MyUserPostInformationReq req) {
        return PageHelper.startPage(req.getPageNum(),req.getPageSize()).doSelectPageInfo(()->{
                this.baseMapper.postInformation(req);
        });

    }

    /**
     * 修改用户信息
     * @param req
     * @return
     */
    @Override
    public boolean edit(MyUserPostInformationEditReq req) {
        AppUser user = this.getById(req.getUserId());
        AppMyUserinfo userinfo = appMyUserinfoService.getById(req.getUserId());
        //AppMyFansFollow follow =appMyFansFollowMapper.selectById(req.getUserId());
        if (Objects.isNull(user) || Objects.isNull(userinfo)) {
            throw new RuntimeException("用户不存在");
        }
        if (!req.getNickName().equals(user.getNickName())) {
            if (this.baseMapper.exists(new LambdaQueryWrapper<AppUser>().eq(AppUser::getNickName, req.getNickName()))) {
                throw new RuntimeException("昵称已存在");
            }
        }
        boolean result =this.baseMapper.updateUsserPost(req);
//        boolean resultUserInfo=true;
//        boolean resultUser=true;
//        if (!req.getSign().equals(userinfo.getSign()) || !req.getHomeimage().equals(userinfo.getHomeimage())) {
//            resultUserInfo = this.baseMapper.updateUserInfo(req);
//        }
//        if (!req.getAvatar().equals(user.getAvatar()) || req.getNickName().equals(user.getNickName())) {
//            resultUser = this.baseMapper.updateUser(req);
//        }
//       return resultUser && resultUserInfo;
         return result;
    }

    /**
     * 分页查询用户帖子
     * @param req
     * @return
     */
    @Override
    public PageInfo<MyUserPostResp> post(MyUserPostInformationReq req) {
//        AppUser user = this.getById(req.getUserId());
//        if (Objects.isNull(user)) {
//            throw new RuntimeException("用户不存在");
//        }
//        List<MyUserPostResp> resp = this.baseMapper.post(req);
        return PageHelper.startPage(req.getPageNum(),req.getPageSize()).doSelectPageInfo(()->{
            this.baseMapper.post(req);
        });
    }

    /**
     * 根据用户id查询用户帖子和个人信息
     * @param userId
     * @return
     */
    @Override
    public MyUserPostHomeResp postByUserId(Long userId) {
        MyUserPostInformationResp infoResp = postInformationByUserId(userId);
        MyUserPostHomeResp resp = new MyUserPostHomeResp();
        resp.setUserId(infoResp.getUserId());
        resp.setNickName(infoResp.getNickName());
        resp.setAvatar(infoResp.getAvatar());
        resp.setFans(infoResp.getFans());
        resp.setFollowers(infoResp.getFollowers());
        resp.setLikes(infoResp.getLikes());
        resp.setSign(infoResp.getSign());
        resp.setHomeimage(infoResp.getHomeimage());
        resp.setStatus(infoResp.getStatus());
        resp.setSex(infoResp.getSex());
        resp.setMyUserPostRespList(postHomeByUserId(userId));
        return resp;
    }


    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    @Override
    public MyUserPostInformationResp postInformationByUserId(Long userId){
        return this.baseMapper.postInformationByUserId(userId);
    }

    @Override
    public MyUserPostResp postByid(Long id) {
        MyUserPostResp resp = appMyUserdynamicMapper.getByPostId(id);
        return resp;
    }

    /**
     * 根据用户id查询帖子
     * @param userId
     * @return
     */

    public List<MyUserPostResp> postHomeByUserId(Long userId){
        return this.baseMapper.postHomeByUserId(userId);
    }

    @Override
    public boolean revise(MyUserPostEditReq req) {
        AppMyUserdynamic db=appMyUserdynamicService.getById(req.getId());
        if(Objects.isNull(db)){
            throw new RuntimeException("帖子不存在");
        }
        return appMyDynamicImgOrVideoMapper.updateinfomation(req);
    }
}




